package weektwo_day03_thread;

import java.io.IOException;
import java.io.RandomAccessFile;

public class VideoThread extends Thread {
	private int length;
	private int pointer;
	private RandomAccessFile raf1;
	private RandomAccessFile raf12;
	
	public VideoThread(int length,int pointer) throws IOException{
		this.length = length;
		this.pointer = pointer;
		this.raf1 = new RandomAccessFile("Ingrid Michaelson - Girls Chase Boys.mp3","r");
		this.raf12 = new RandomAccessFile("D:/Ingrid Michaelson - Girls Chase Boys.mp3","rw");
		raf12.seek(pointer);
		raf1.seek(pointer);
	}
	@Override
	public void run() {
		byte[] buf = new byte[1024];
		
		int m = 0;
		try {
			int downloadsize = 0;
			
			while((m = raf1.read(buf)) != -1){
				downloadsize += m;
				if((length - downloadsize) <= buf.length){
					raf12.write(buf, 0, length - downloadsize);          
					break;
				}
				raf12.write(buf, 0, m);
			}
			raf1.close();
			raf12.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
